<?php

namespace Jinxing\Admin\Composers;

use Jinxing\Admin\Traits\UserTrait;
use Illuminate\Contracts\View\View;
use Jinxing\Admin\Repositories\MenuRepository;

class MenusComposer
{
    use UserTrait;

    /**
     * @var MenuRepository
     */
    private $menuRepository;

    public function __construct(MenuRepository $menuRepository)
    {
        $this->menuRepository = $menuRepository;
    }

    /**
     * Bind data to the view.
     *
     * @param View $view
     *
     * @return void
     * @throws \Exception
     */
    public function compose(View $view)
    {
        $uid = $this->getUser('id');
        // 从缓存中获取菜单
        if (!$menus = cache()->store('file')->get('admin:users:menus:' . $uid)) {
            $menus = $this->menuRepository->getPermissionMenus($uid);
            cache()->store('file')->put('admin:users:menus:' . $uid, $menus, 10);
        }

        $view->with('menus', $menus);
    }
}
